
var api = require('../../utils/api.js')
var $ = require('../../utils/util.js')
var regeneratorRuntime = require('../../utils/runtime.js')
var app = getApp()
var Store = app.Store 
Page({
  data: {
    showPrivacy: false,
    src:'',//语音播报路径
    islogin:0,//是否登录
    show:false,//是否弹出需要登录的提示
    banners: [
      { img: api.imageUrl + '/assets/banner1.png', url:'' },
      { img: api.imageUrl + '/assets/banner2.png', url: '/pages/pollingManage/pollingManageIndex/pollingManageIndex' }
    ],
    list: [
      { name: '工作票', img:api.imageUrl + '/assets/n_11.png', num: '0', url: '/pages/workTicket/workIndex/workIndex' }, 
      { name: '操作票', img:api.imageUrl + '/assets/n_5.png', num: '0', url: '/pages/operationTicket/operationIndex/operationIndex' }, 
      // { name: '锁群管理', img:api.imageUrl + '/assets/n_8.png', num: '0', url: '/pages/lockMange/lockIndex/lockIndex' },
      { name: '蓝牙锁', img:api.imageUrl + '/assets/n_8.png', num: '0', url: '/pages/lockMange/lockScan/lockScan' },
      { name: '告警管理', img:api.imageUrl + '/assets/n_1.png', num: '0', url:'/pages/warningManage/warningManageIndex/warningManageIndex' }, 
      { name: '巡检管理', img:api.imageUrl + '/assets/n_2.png', num: '0', url: '/pages/pollingManage/pollingManageIndex/pollingManageIndex' }, 
      { name: '报修管理', img:api.imageUrl + '/assets/n_3.png', num: '0', url: '/pages/repairsManage/repairsManageIndex/repairsManageIndex' }, 
      { name: '报修派单', img:api.imageUrl + '/assets/n_11.png', num: '0', url: '/pages/serve/serveRepairs/serveRepairs'}, 
      { name: '抢修管理', img:api.imageUrl + '/assets/n_4.png', num: '0', url: '/pages/robManage/robManageIndex/robManageIndex' }, 
      { name: '抢修派单', img:api.imageUrl + '/assets/n_5.png', num: '0', url: '/pages/robManage/robManageRushSend/robManageRushSend' }, 
      { name: '缺陷管理', img:api.imageUrl + '/assets/n_6.png', num: '0', url: '/pages/defectManage/defectManageIndex/defectManageIndex' }, 
      { name: '检修管理', img:api.imageUrl + '/assets/n_7.png', num: '0', url: '/pages/checkManage/checkManageIndex/checkManageIndex' },
      // { name: '监控管理', img:api.imageUrl + '/assets/n_10.png', num: '0', url: '/pages/onlineVideo/onlineVideoIndex/onlineVideoIndex' },
      
      // { name: '任务管理', img:api.imageUrl + '/assets/n_11.png', num: '0', url: '/pages/task/taskIndex/taskIndex' },
      // { name: '在线遥控', img:api.imageUrl + '/assets/n_12.png', num: '0', url: '/pages/onlineMonitoring/onlineMonitoringYK/onlineMonitoringYK' },
      // { name: '宿舍监管', img:api.imageUrl + '/assets/n_18.png', num: '0', url: '/pages/dormitoryElectricity/dormitoryElectricity' },
      // { name: '故障录波', img:api.imageUrl + '/assets/n_13.png', num: '0', url: '/pages/wave/waveIndex/waveIndex' },
      // { name: '主接线图', img:api.imageUrl + '/assets/n_15.png', num: '0', url: '/pages/mainWrite/mainWrite' },
      // { name: '主接线图测试', img:api.imageUrl + '/assets/n_15.png', num: '0', url: '/pages/drawingTool/drawingTool' },
      { name: '工程管理', img:api.imageUrl + '/assets/n_19.png', num: '0', url: '/pages/projectManage/projectManageIndex/projectManageIndex' },
      // { name: '资产管理', img:api.imageUrl + '/assets/n_16.png', num: '0', url: '/pages/document/index/index' },

    ],
    // lysurl: api.imageUrl + '/assets/n_8.png'
  },
  onLoad: function (options) {
    console.log('++++++++++',Date.now(),options)
    // if ('token' in options&&'expires' in options){
    //   Date.now()>options.expires?wx.setStorageSync('token', options.token):wx.setStorageSync('token', 0)
    //   console.log('options',options.token)
    // }
    if ('sign' in options&&'phone' in options){
      this.getAppSign(options)
    }else{
      this.ifToken()
    }
    console.log('getStorageSync',wx.getStorageSync('token'))
    
    wx.getPrivacySetting({
      success: res => {
       // 返回结果为: res = { needAuthorization: true/false, privacyContractName: '《xxx隐私保护指引》' }
        if (res.needAuthorization) {
          // 需要弹出隐私协议
          this.setData({
            showPrivacy: true
          })
          wx.hideTabBar();
        } else {
          // 用户已经同意过隐私协议，所以不需要再弹出隐私协议，也能调用已声明过的隐私接口
          // wx.getUserProfile()
          // wx.chooseMedia()
          // wx.getClipboardData()
          // wx.startRecord()


        }
      },
      fail: () => {},
      complete: () => {}
    })
    
    // this.autoUpdate()
  },
  onShow: function () {
    // console.log('----------',options)
    // var user_id = wx.getStorageSync('user_id') || 0;
    var token = wx.getStorageSync('token') || 0;
    if(token){
      this._index() 
    }
    // if (!user_id&&!token) {
    //     this.setData({
    //       islogin:0
    //     })
    //     //隐藏底部菜单
    //     wx.hideTabBar();
    // } else {
    //   this.setData({
    //     islogin:1
    //   })
    //   wx.showTabBar();
    //   // this._index() // 首页统计数据
    //   var locationIgnore=wx.getStorageInfoSync("locationIgnore") ||"false";
    //   if(locationIgnore!="true"){
    //       实时定位
    //       app.LocationUpdateBackground();
    //       app.updateLocation();
    //       //初始获取定位权限
    //       app.realTimeLocation();
    //   }

    // }
  },
  getAppSign(options){
    console.log('获取到options',options)
    var that=this
    api.$post(api.main.refreshToken, {
      timestamp: options.timestamp,
      phone: options.phone,
      randomstr: options.randomstr,
      token: options.sign
    }).then((res) => {
      console.log('跳转接口返回',res)
      if(res.code==200){
        wx.setStorageSync('user_id', res.data.userId) 
        wx.setStorageSync('token', res.data.access_token)
        wx.setStorageSync('user_name', that.data.username)
        api.$get(api.main.userInfo).then((userInfo) => {
          wx.setStorageSync('avatar', userInfo.data.avatar) 
          wx.setStorageSync('sex', userInfo.data.sex) 
          wx.setStorageSync('phonenumber', userInfo.data.phonenumber)
          that.ifToken()
        })
      }
      
    })
  },
  ifToken(){
    var token = wx.getStorageSync('token') || 0;
    var uname =wx.getStorageSync('user_name')
    if(token){
      //实时定位
      if(uname!='胡志宏'){
        app.LocationUpdateBackground();
        app.updateLocation();
      }
      this.setData({
        islogin:0
      })
      this._index() 
      wx.showTabBar();
      api.$get(api.main.userRoleList).then((res) => {
        if(res.code==200){
          Store.dispatch({
            type: 'SET_USER_ROLE',
            userRoleList: res.data,
          })
        }
      })
      api.$get(api.main.userSignature).then((res) => {
        if(res.code==200){
          wx.setStorageSync('signUrl', res.data.signature)
        }
      })
    }else{
      this.setData({
        islogin:1
      })
      //隐藏底部菜单
      wx.hideTabBar();
    }
  },
  _index(){
    var list = this.data.list
    return new Promise((resolve, reject) => {  //首页统计数据 
      api.$get(api.main.index).then((res) => { 
        list[3].num = res.data.event
        list[4].num = res.data.patrolplan 
        list[5].num = res.data.reportRepair
        list[7].num = res.data.rushRepair
        list[9].num = res.data.defect
        list[10].num = res.data.checkRepair
        // list[7].num = res.data.online_video
        // list[8].num = res.data.task
        this.setData({
          list
        })
      })
    })
  },
  toLinkHandle(e){ // 去跳转各个页面
    var user_id = wx.getStorageSync('user_id') || 0;
    var token = wx.getStorageSync('token') || 0;
    console.log(token)
    if(!user_id&&!token){
        this.setData({
          show:true,
          islogin:0
        })
    }else{
      var {url} = $.detail(e)
      $.linkurl(url)
    }
  },
  toMiniprogram(e) { // 去跳转小程序
    wx.navigateToMiniProgram({
      appId: 'wx7c2399e2a922e196',
      extraData: {
        'user_id': wx.getStorageSync('user_id')
      },
      success(res){
        // console.log("跳转成功")
      }
    })
  },
  urlLinkHandle(e){
    var user_id = wx.getStorageSync('user_id') || 0;
    if(!user_id){
        this.setData({
          show:true,
          islogin:false
        })
    }else{
      var { url } = $.detail(e);
      if (url){
        $.linkurl(url)
      }
    }
  }, 
  tologin(){
    //去登录页面
    $.linkurl(`/pages/main/login/login`)
  },
  handleAgreePrivacyAuthorization() {
    // 用户同意隐私协议事件回调
    // 用户点击了同意，之后所有已声明过的隐私接口和组件都可以调用了
    // wx.getUserProfile()
    // wx.chooseMedia()
    // wx.getClipboardData()
    // wx.startRecord()
    this.setData({
      showPrivacy: false
    })
    wx.showTabBar();
  },
  canclePrivacyAuthorization(){
    this.setData({
      showPrivacy: false
    })
    $.toast('用户拒绝隐私授权, 部分功能将不能使用')
  },
  handleOpenPrivacyContract() {
    // 打开隐私协议页面
    wx.openPrivacyContract({
      success: () => {}, // 打开成功
      fail: () => {}, // 打开失败
      complete: () => {}
    })
  }


})